#include "protocol.h"
#include "boot.h"
#include "tools.h"
#include "msg.h"
#include "variable.h"

#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "croutine.h"


#include "task_iic.h"
#include "task_spi.h"
#include "task_uart.h"
#include "task_adc.h"
#include "task_dac.h"
#include "task_io.h"
#include "task_key.h"
#include "task_pwm.h"

#include "linkediic.h"
#include "linkediicR.h"

/*****************************************************
消息处理，把数据帧转换成具体数据
*****************************************************/
void protocolDeal(MSG_T msg)
{
	if(msg.type==CMD_TASK_CONFIG)
	{
		if(msg.buff[1]==TASK_ON)		
		{
			if(msg.buff[0]==TASK_IO)					xTaskCreate( vTask_GPIO, 	 		"vTask_GPIO",  200,  NULL,  4, &xHandleTaskGPIO );
			else if(msg.buff[0]==TASK_ADC)		xTaskCreate( vTask_ADC, 	 		"vTask_ADC",   200,  NULL,  4, &xHandleTaskADC );
			else if(msg.buff[0]==TASK_DAC)		xTaskCreate( vTask_DAC, 	 		"vTask_DAC",   200,  NULL,  4, &xHandleTaskDAC );
			else if(msg.buff[0]==TASK_PWM)		xTaskCreate( vTask_PWM, 	 		"vTask_PWM",   200,  NULL,  4, &xHandleTaskPWM );
			else if(msg.buff[0]==TASK_IIC)		xTaskCreate( vTask_IIC, 	 		"vTask_IIC",   200,  NULL,  4, &xHandleTaskIIC );
			else if(msg.buff[0]==TASK_SPI)		xTaskCreate( vTask_SPI, 	 		"vTask_SPI",   200,  NULL,  4, &xHandleTaskSPI );
			else if(msg.buff[0]==TASK_KEY)		xTaskCreate( vTask_KEY, 	 		"vTask_KEY",   200,  NULL,  4, &xHandleTaskKEY );
		}
		else if(msg.buff[1]==TASK_CLOSE)		
		{
			if(msg.buff[0]==TASK_IO)					vTaskDelete(xHandleTaskGPIO);
			else if(msg.buff[0]==TASK_ADC)		vTaskDelete(xHandleTaskADC);
			else if(msg.buff[0]==TASK_DAC)		vTaskDelete(xHandleTaskDAC);
			else if(msg.buff[0]==TASK_PWM)		vTaskDelete(xHandleTaskPWM);
			else if(msg.buff[0]==TASK_IIC)		vTaskDelete(xHandleTaskIIC);
			else if(msg.buff[0]==TASK_SPI)		vTaskDelete(xHandleTaskSPI);
			else if(msg.buff[0]==TASK_KEY)		vTaskDelete(xHandleTaskKEY);
		}
	}
	else if(msg.type==CMD_IO_CONFIG)
	{
		for(uint8_t i=0;i<GPIO_NUM;i++)
		{
			ioStructList.io[i].type = msg.buff[i];
		}
	}
	else if(msg.type==CMD_IO_OUT)
	{
			ioStructList.io[msg.buff[0]].stat = msg.buff[1];
	}
	else if(msg.type==CMD_DAC_VALUE)
	{
			memcpy(&dac, msg.buff, 4);
	}
	else if(msg.type==CMD_PWM_CONFIG)
	{
			pwmStruct.fre = (uint16_t)msg.buff[0]<<8 | msg.buff[1];
	}
	else if(msg.type==CMD_PWM_VALUE)
	{
			pwmStruct.duty1 = msg.buff[0];
			pwmStruct.duty2 = msg.buff[1];
	}
	else if(msg.type==CMD_IIC_WRITE)
	{
			Linkediic_insertNode(msg.buff[0], msg.buff[1], msg.buff[2]); 
	}
	else if(msg.type==CMD_IIC_READ)
	{
			LinkediicR_insertNode(msg.buff[0], msg.buff[1]); 
	}
}


			

			

			
